function rfd = fixed_dig(r,k)
%
%rfd = FIXED_DIG(r,k);
%
% Representation of a real number r (in float form) with k significant 
% digits.
% The output rfd is a string.
% If k is unassigned od invalid (it must be a positive integer), 
% k = 3 is assumed.
%
% It uses TEST_INT.

% G. Teza, 2005

if nargin == 1, k = 3; end
if ~test_int(k), k = 3; end

rfd = num2str(r);
lr = length(rfd);
if strcmp(rfd,'0') == 1   % In this case, r is 0
    adj = [];
    for m = 1:k 
        adj = [adj '0'];
    end
    rfd = [ '0.' adj];
    return
end

n = 1;
while (strcmp(rfd(n),'0') == 1)||(strcmp(rfd(n),'.') == 1)
    n = n+1;
end

if n < lr-k+1
    rfd(n+k:lr) = []; % Digits with <= until n+k-1 (they are k!) must be keeped!
else
        % In this case, some 0 must be adjoint in order to
        % obtain k significant digits
    adj = [];
    for nn = 1:n+k-lr-1
        adj = [adj '0'];
    end 
    rfd = [rfd adj];
end  